<?php
class Expert_model extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }
    /**
     * 获取用户行家
     * @param $target_id
     * @param $target_type
     */
	public function find_by_user_id($user_id) {
		$this->db->select('id,title');
		$this->db->where('created_by', intval($user_id));
		return $this->db->get('experts')->result_array();
		return $result;
	}
  	/**
     * 获取用户行家
     * @param $target_id
     * @param $target_type
     */
	public function get_count_by_user_id($user_id) {
		$this->db->where('created_by', intval($user_id));
		$this->db->from('experts');
		return $this->db->count_all_results();
	}
	/**
	 * 获取单个行家
	 * @param int $id
	 */
	public function find_by_id($id) {
		$sql = "select  p.province,c.city,a.area,e.* from experts e 
		left join province p on e.province_id=p.provinceID
		left join city c on e.city_id=c.cityID
		left join area a on e.area_id=a.areaID
		where e.id=" . $id;
		$result = $this->db->query($sql)->row_array();
		return $result;
	}
	/**
	 * 创建新的行家
	 * @param array $data 技能相关数组
	 */
	public function add($data) {
		if (!empty($data['id'])) {
			$expert = $this->find_by_id($data['id']);
			$this->db->where('id', $data['id']);
			$this->db->update('experts', $data); 
			if ($data['category_id'] != $expert['category_id']) {
				$sql = "update categories set expert_count=expert_count+1 where id=" . intval($data['category_id']);
				$this->db->query($sql);
				$sql = "update categories set expert_count=expert_count-1 where id=" . intval($expert['category_id']);
				$this->db->query($sql);
			}
			return $data['id'];
		} else {
			$this->db->insert("experts", $data);
			$new_expert_id = $this->db->insert_id();
			$sql = "update categories set expert_count=expert_count+1 where id=" . intval($data['category_id']);
			$this->db->query($sql);
			return $new_expert_id;
		}
		
	}
	/**
	 * 删除某个行家
	 * @param int $id
	 */
	public function delete($id) {
		$id = intval($id);
		$expert = $this->find_by_id($id);
		$this->db->delete('experts', array('id' => $id)); 
		$sql = "update categories set expert_count=expert_count-1 where id=" . intval($expert['category_id']);
		$this->db->query($sql);
	}
	
	/**
	 * 
	 * 根据条件获取行家数目
	 * @param string $condition 条件
	 */
	
	public function get_count_by_condition($condition) {
		$sql = "select count(*) as total from experts e where 1=1 and " . $condition;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	
	/**
	 * 
	 * 获取行家信息
	 * @param string $condition
	 * @param int $offset
	 * @param int $limit
	 */
	public function find_by_condition($condition, $offset = 0, $limit = 10) {
		$sql = "select e.id,e.title,e.price,e.province_id,e.city_id,e.area_id,e.created_by,u.nickname,u.thumb_avatar from experts e 
		left join users u on u.id=e.created_by
		where 1=1 and " . $condition;
		$sql .= " limit " .$offset . "," . $limit;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	
	/**
	 * 
	 * 根据条件获取行家数目
	 * @param string $condition 条件
	 */
	
	public function get_count_by_condition_search($sql) {
		//print $sql;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	
	/**
	 * 
	 * 获取行家信息
	 * @param string $condition
	 * @param int $offset
	 * @param int $limit
	 */
	public function find_by_condition_search($sql, $offset = 0, $limit = 10) {
		
		$sql .= " limit " .$offset . "," . $limit;
		//print $sql;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
}